using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ByHand.Model;
using ByHand.Data;
using ByHand.Services.Interfaces;
using ByHand.Data.Interfaces;
using ByHand.Services.Code;

namespace ByHand.Services.Implements
{
    public class ProviderService : RepositoryBase<Provider>, IProviderService
    {
        public ProviderService(IDatabaseFactory DbFactory) : base(DbFactory) { }


        public List<Provider> GetByPage(int page, int pagesize, out int total)
        {
            var list = from u in ByHandDB.Providers
                       orderby u.ID descending
                       select u;
            int skip = (page - 1) * pagesize;
            total = list.ToList().Count;
            return list.Skip(skip).Take(pagesize).ToList();
        }

        public List<Provider> GetByPageFilter(int page, int pagesize, out int total, string filter)
        {
            int skip = (page - 1) * pagesize;
            var list = (from d in ByHandDB.Providers
                        where d.Name.Contains(filter) || d.ProviderCode.Contains(filter)
                        orderby d.ID descending
                        select d);

            total = list.ToList().Count;
            var alist = list.ToList();
            return list.Skip(skip).Take(pagesize).ToList();
        }


        public bool CheckCodeExist(string code)
        {
            var chk = (from d in ByHandDB.Providers
                       where d.ProviderCode.Equals(code)
                       select d).ToList();
            if (chk.Count > 0) return true;
            return false;
        }

        public List<Provider> GetAllAvaiable()
        {
            return (from d in ByHandDB.Providers
                    where !d.IsDeleted
                    select d).ToList();
        }
        public Provider GetByMerchant(int merchanID)
        {
            return (from d in ByHandDB.Providers
                    where !d.IsDeleted && d.MerchantID == merchanID
                    select d).FirstOrDefault();
        }
    }
}
